package com.adinnet.api.word.service.impl;

import com.adinnet.api.childMaxStudy.mapper.ChildMaxStudyMapper;
import com.adinnet.api.childMaxStudy.model.ChildMaxStudy;
import com.adinnet.api.word.model.Word;
import com.adinnet.api.word.mapper.WordMapper;
import com.adinnet.api.word.service.WordService;
import com.adinnet.core.JsonResult;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 重点词汇表 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-02
 */
@Service
public class WordServiceImpl extends ServiceImpl<WordMapper, Word> implements WordService {

    @Autowired
    private WordMapper wordMapper;

    @Autowired
    private ChildMaxStudyMapper childMaxStudyMapper;

    /**
     * 查询词汇详情
     * @param id
     * @return
     */
    @Override
    public JsonResult wordDetails(Integer id) {
        Word word = wordMapper.selectById(id);
        if (null == word) {
            return JsonResult.ok().put("data", new JSONObject());
        }
        QueryWrapper<ChildMaxStudy> wrapper = new QueryWrapper<>();
        wrapper.eq("type", 3).eq("third_id", id).eq("deleted", 0);
        ChildMaxStudy childMaxStudy = childMaxStudyMapper.selectOne(wrapper);
        if (null == childMaxStudy) {
            word.setHistoryScore(0);
        } else {
            word.setHistoryScore(childMaxStudy.getPeaseNum());
        }
        return JsonResult.ok().put("data", word);
    }
}
